<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
 * 
 * @author 系统菜单管理
 * yq
 */
class MenuModel extends Model{
    /**
     * 获取全部菜单
     * @param unknown $db_name 表名
     * @return array
     */
    public function getAllMenu($db_name)
    {
        return Db::name($db_name)->order('sort asc,id asc')->select();
    }
    
    
    
    /**
     * 系统菜单栏
     * @param unknown $cate 节点数据
     * @param string $lefthtml
     * @param number $pid
     * @param number $lvl
     * @param number $leftpin
     * @return array|unknown[]
     */
    public function rule($cate , $lefthtml = ' — ' , $pid=0 , $lvl=0, $leftpin=0 ){ 
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $v['lvl']=$lvl + 1;
                $v['leftpin']=$leftpin + 0;//左边距
                $v['lefthtml']=str_repeat($lefthtml,$lvl);
                $arr[]=$v;
                $arr= array_merge($arr,$this->rule($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
            }
        }
        return $arr;
    }
    
    
    /**
     * 修改系统菜单栏
     * @param unknown $where 条件
     * @param array $data  修改数据
     * @return Boolean true|false
     */
    public function modify($where,array $data,$db_name){
        
        $data['update_time'] = time(); //修改时间
        $id = Db::name($db_name)->where($where)->update($data);
        //写入log日志
        if(empty($id)){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】修改系统菜单栏失败【id='.$where['id'].'】',0,$this->user_data['model'].'_log');
        }else{
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】修改系统菜单栏成功 【id='.$where['id'].'】',1,$this->user_data['model'].'_log');
        }
        return $id;
    }
    
    
    /**
     * 查询一条菜单栏信息
     * @param unknown $where 条件
     * @param unknown $field 查询字段
     * @param unknown $db_name 表名
     * @return array
     */
    public function getOneMenu($where,$field ='',$db_name){
        return Db::name($db_name)->field($field)->where($where)->find();
    }
    
    
    /**
     * 
     * @param unknown $db_name 表名
     * @param array $data  添加数据
     * @return id|int  返回ID
     */
    public function AddMenu($db_name,array $data){
        $data['add_time'] = time();//添加时间
        $id =  Db::name($db_name)->insertGetId($data);
        if(empty($id)){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加系统菜单栏失败',0,$this->user_data['model'].'_log');
        }else{
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加系统菜单栏成功',1,$this->user_data['model'].'_log');
        }
        return $id;
    }
    
    /**
     * 系统菜单栏删除
     * @param unknown $where 条件
     * @param unknown $db_name 表名
     * @return Boolean true|false
     */
    public function del($where,$db_name){
        $res = Db::name($db_name)->where($where)->delete();
        if(!empty($res)){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】删除系统菜单栏成功',1,$this->user_data['model'].'_log');
        }
        return $res;
    }
    
    /**
     * 按条件查询多条
     * @param unknown $where 条件
     * @param unknown $db_name 表名
     * @return array
     */
    public function getSubmenu($pid,$db_name,$nodeStr='')
    {
        $where =  empty($nodeStr) ? 'status = 1 and menutitle=1 and pid='.$pid : 'status = 1 and menutitle=1 and pid='.$pid .' and id in('.$nodeStr.')';
        $order = 'sort ASC';
        $res = Db::name($db_name)->where($where)->order($order)->select();
        return $res;
    }
    
    
    
    
    
    
}